package cn.itcast.goods.cart.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import cn.itcast.goods.book.domain.Book;
import cn.itcast.goods.cart.domain.CartItem;
import cn.itcast.goods.user.domain.User;
import cn.mytools.commons.CommonUtils;
import cn.mytools.jdbc.TxQueryRunner;

public class CartItemDao {
	private QueryRunner qr = new TxQueryRunner();

	/**
	 * 把一个map转成cartItem
	 * 
	 * @param map
	 * @return
	 */
	private CartItem toCartItem(Map<String, Object> map) {
		if (map == null)
			return null;
		CartItem cartItem = CommonUtils.toBean(map, CartItem.class);
		Book book = CommonUtils.toBean(map, Book.class);
		User user = CommonUtils.toBean(map, User.class);
		cartItem.setBook(book);
		cartItem.setUser(user);
		return cartItem;
	}

	/**
	 * 把多个购物车条目转成一个List<CartItem>
	 * 
	 * @param mapList
	 * @return
	 */
	private List<CartItem> toCartItemList(List<Map<String, Object>> mapList) {
		List<CartItem> cartItemList = new ArrayList<CartItem>();
		for (Map<String, Object> map : mapList) {
			CartItem cartItem = toCartItem(map);
			cartItemList.add(cartItem);
		}
		return cartItemList;
	}
	/**
	 * 查询指定用户的购物车条目
	 * @param uid
	 * @return
	 * @throws SQLException
	 */
	public List<CartItem> findByUid(String uid) throws SQLException {
		String sql = "select * from t_cartitem c , t_book b where c.bid = b.bid and c.uid = ? order by c.orderby";
		List<Map<String, Object>> mapList = qr.query(sql, new MapListHandler(), uid);
		return toCartItemList(mapList);
	}
	/**
	 * 通过uid和bid 查找
	 * @param uid
	 * @param bid
	 * @return
	 * @throws SQLException
	 */
	public CartItem findByUidAndBid(String uid, String bid) throws SQLException {
		String sql = "select * from t_cartitem where uid = ? and bid = ?";
		CartItem cartItem = qr.query(sql, new BeanHandler<CartItem>(CartItem.class), uid, bid);
		return cartItem;
	}
	/**
	 * 更新数量
	 * @param cartItemId
	 * @param totalQuantity
	 * @throws SQLException
	 */
	public void updateQuantity(String cartItemId, int totalQuantity) throws SQLException {
		String sql = "update t_cartitem set quantity = ? where cartItemId = ? ";
		qr.update(sql, totalQuantity, cartItemId);

	}
	/**
	 * 添加购物车条目
	 * @param cartItem
	 * @throws SQLException
	 */
	public void addCartItem(CartItem cartItem) throws SQLException {
		String sql = "insert into t_cartitem values(?,?,?,?,?)";
		Object[] params = { cartItem.getCartItemId(), cartItem.getQuantity(), cartItem.getBook().getBid(),
				cartItem.getUser().getUid(), null };
		qr.update(sql, params);

	}
	/**
	 * 批量删除购物车条目
	 * @param cartItemIds
	 * @throws SQLException
	 */
	public void batchDeleteCartItem(String cartItemIds) throws SQLException {
		Object[] cartItemArray = cartItemIds.split(",");
		String whereSql = toWhereSql(cartItemArray.length);
		String sql = "delete from t_cartitem where "+whereSql;
		qr.update(sql, cartItemArray);//cartItemArray必须是Object 类型
	}
	/**
	 * 
	 * 生成where子句
	 * @param len
	 * @return
	 */
	private String toWhereSql(int len) {
		StringBuilder sb = new StringBuilder("cartItemId in (");
		for (int i = 0; i < len; i++) {
			sb.append("?");
			if(i < len -1 ){
				sb.append(",");
			}
		}
		sb.append(")");
		return sb.toString();
	}
	/**
	 * 按条目ID查询
	 * @param cartItemId
	 * @return
	 * @throws SQLException 
	 */
	public CartItem findByCartItemId (String cartItemId) throws SQLException {
		String sql = "select * from t_cartItem c , t_book b where c.bid = b.bid and c.cartItemId=?";
		Map<String, Object> map = qr.query(sql,new MapHandler(),cartItemId);
		return toCartItem(map);
	}

	public List<CartItem> loadCartItems(String cartItemIds) throws SQLException {
		Object[] cartItemArray = cartItemIds.split(",");
		String whereSql = toWhereSql(cartItemArray.length);
		String sql = "select * from t_cartitem c , t_book b where c.bid = b.bid and "+whereSql;
		List<Map<String,Object>> mapList = qr.query(sql, new MapListHandler(),cartItemArray);
		return toCartItemList(mapList);
		
	}
}
